﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using nvn.Core.MVP;
using Hutech.CRM.Library.Views;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Utilities;
using nvn.Library.Patterns;
using System.Drawing;
using Hutech.Authentication.Library.Web.Framework;

namespace Hutech.CRM.Web.Controls.ManageCustomers
{
    public partial class SearchCustomerControl : System.Web.UI.UserControl, ISearchCustomerView
    {
        #region MEMBER VARIABLES
        public int _PageSize;
        private int _TotalPages = 1;
        private SearchEntity _SearchEntity;
        #endregion

        #region Cac su kien uy thac
        public event EventHandler<OneParameterEventAgrs<SearchEntity>> TranferData;
        protected void onTranferData(SearchEntity parameterValue)
        {
            if (TranferData != null)
            {
                TranferData(this, new OneParameterEventAgrs<SearchEntity>(parameterValue));
            }
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.InitView();
            ImageButtonSearch.Click += new ImageClickEventHandler(ImageButtonSearch_Click);
            DropDownListJobs.SelectedIndexChanged += new EventHandler(DropDownListJobs_SelectedIndexChanged);
            DropDownListLevels.SelectedIndexChanged += new EventHandler(DropDownListLevels_SelectedIndexChanged);
            DropDownListPositions.SelectedIndexChanged += new EventHandler(DropDownListPositions_SelectedIndexChanged);
            DropDownListProvinces.SelectedIndexChanged += new EventHandler(DropDownListProvinces_SelectedIndexChanged);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                OnFristLoadEventHandler();
        }
        public SearchEntity SearchEntity
        {
            get 
            {
                SearchEntity item = new SearchEntity();
                item.TextSearch = TextSearch;
                try
                {
                    item.Job = new Guid(DropDownListJobs.SelectedValue);
                    item.Position = new Guid(DropDownListPositions.SelectedValue);
                    item.Province = new Guid(DropDownListProvinces.SelectedValue);
                    item.Level = new Guid(DropDownListLevels.SelectedValue);
                }
                catch (Exception)
                {
                    throw;
                }

                return item; 
            }
            set
            {
                SearchEntity item = new SearchEntity();
                item.TextSearch = TextSearch;
                try
                {
                    item.Job = new Guid(DropDownListJobs.SelectedValue);
                    item.Position = new Guid(DropDownListPositions.SelectedValue);
                    item.Province = new Guid(DropDownListProvinces.SelectedValue);
                    item.Level = new Guid(DropDownListLevels.SelectedValue);
                }
                catch (Exception)
                {
                    throw;
                }

                _SearchEntity = item;
            }
        }

        #region Event
        void DropDownListProvinces_SelectedIndexChanged(object sender, EventArgs e)
        {
            onTranferData(this._SearchEntity);
        }
        void DropDownListPositions_SelectedIndexChanged(object sender, EventArgs e)
        {
            onTranferData(this._SearchEntity);
        }
        void DropDownListLevels_SelectedIndexChanged(object sender, EventArgs e)
        {
            onTranferData(this._SearchEntity);
        }
        void DropDownListJobs_SelectedIndexChanged(object sender, EventArgs e)
        {
            onTranferData(this._SearchEntity);
        }
        void ImageButtonSearch_Click(object sender, ImageClickEventArgs e)
        {
            onTranferData(null);
        }
        #endregion

        #region Implement from View class
        #region Property
        public string TextSearch
        {
            set { TextBoxSearch.Text = value; }
            get { return TextBoxSearch.Text; }
        }

        public List<TowTypeParameters<Guid, string>> Levels
        {
            set
            {
                DropDownListLevels.Items.Clear();
                DropDownListLevels.DataSource = value;
                DropDownListLevels.DataValueField = "T";
                DropDownListLevels.DataTextField = "V";
                DropDownListLevels.DataBind();
            }
        }
        public List<TowTypeParameters<Guid, string>> Jobs
        {
            set
            {
                DropDownListJobs.Items.Clear();
                DropDownListJobs.DataSource = value;
                DropDownListJobs.DataValueField = "T";
                DropDownListJobs.DataTextField = "V";
                DropDownListJobs.DataBind();
            }
        }
        public List<TowTypeParameters<Guid, string>> Provinces
        {
            set
            {
                DropDownListProvinces.Items.Clear();
                DropDownListProvinces.DataSource = value;
                DropDownListProvinces.DataValueField = "T";
                DropDownListProvinces.DataTextField = "V";
                DropDownListProvinces.DataBind();
            }
        }
        public List<TowTypeParameters<Guid, string>> Positions
        {
            set
            {
                DropDownListPositions.Items.Clear();
                DropDownListPositions.DataSource = value;
                DropDownListPositions.DataValueField = "T";
                DropDownListPositions.DataTextField = "V";
                DropDownListPositions.DataBind();
            }
        }

        public string ErrorMessage
        {
            set { throw new NotImplementedException(); }
        }

        #endregion

        #region Event
        public event EventHandler<EventArgs> FristLoadEventHandler;
        private void OnFristLoadEventHandler()
        {
            if (FristLoadEventHandler != null)
                FristLoadEventHandler(this, new EventArgs());
        }
        //public event EventHandler<OneParameterEventAgrs<string>> Search_Click;
        //private void OnSearch_Click(string parameterValue)
        //{
        //    if (Search_Click != null)
        //        Search_Click(this, new OneParameterEventAgrs<string>(parameterValue));
        //}

        //public event EventHandler<OneParameterEventAgrs<Guid>> SelectedIndexChanged_EventArgs;
        //private void OnSelectedIndexChanged_EventArgs(Guid parameterValue)
        //{
        //    if (SelectedIndexChanged_EventArgs != null)
        //        SelectedIndexChanged_EventArgs(this, new OneParameterEventAgrs<Guid>(parameterValue));
        //}
        #endregion
        #endregion
    }
}
